﻿using System.Windows.Forms;
using System.ComponentModel;
using Mentrixmax.Comuns.Entidades.Interface;
using System.Collections;
using MentrixmaxFormularios;
using System;
using MentrixmaxFormularios.Properties;
using System.Drawing;
namespace MentrixmaxFormularios.FormsAbstratos
{
    public class MiniCrud : UserControl
    {
        // Fields
        private Button btnAdd;
        private Button btnCancelar;
        private Button btnRemover;
        private Button btnSalvar;
        private IContainer components = null;
        private IEntidade PEntidade;
        private DataGridView PGrid;
        private IList PListaRemovida;
        private FormPai PPai;

        // Events
        [Category("Action"), Description("Fires when the Submit button is clicked.")]
        public event SubmitClickedHandler AddClicked;

        [Category("Action"), Description("Fires when the Submit button is clicked.")]
        public event SubmitClickedHandler CancelClicked;

        [Category("Action"), Description("Fires when the Submit button is clicked.")]
        public event SubmitClickedHandler GridSelected;

        [Category("Action"), Description("Fires when the Submit button is clicked.")]
        public event SubmitClickedHandler RemoveClicked;

        [Description("Fires when the Submit button is clicked."), Category("Action")]
        public event SubmitClickedHandler UpdateClicked;

        // Methods
        public MiniCrud()
        {
            this.InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                this.btnAdd.Enabled = true;
                this.btnRemover.Enabled = false;
                this.btnSalvar.Enabled = false;
                this.btnCancelar.Enabled = false;
                this.OnAddClicked();
            }
            catch
            {
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                this.btnAdd.Enabled = true;
                this.btnRemover.Enabled = false;
                this.btnSalvar.Enabled = false;
                this.btnCancelar.Enabled = false;
                this.OnCancelClicked();
            }
            catch
            {
            }
        }

        private void btnRemover_Click(object sender, EventArgs e)
        {
            this.OnRemoveClicked();
        }

        private void btnSalvar_Click(object sender, EventArgs e)
        {
            this.OnUpdateClicked();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.btnSalvar = new Button();
            this.btnCancelar = new Button();
            this.btnRemover = new Button();
            this.btnAdd = new Button();
            base.SuspendLayout();
            this.btnSalvar.BackgroundImage = Resources.Check_32x32;
            this.btnSalvar.BackgroundImageLayout = ImageLayout.Zoom;
            this.btnSalvar.Enabled = false;
            this.btnSalvar.Location = new Point(0x3e, 0);
            this.btnSalvar.Name = "btnSalvar";
            this.btnSalvar.Size = new Size(0x18, 0x18);
            this.btnSalvar.TabIndex = 10;
            this.btnSalvar.UseVisualStyleBackColor = true;
            this.btnSalvar.Click += new EventHandler(this.btnSalvar_Click);
            this.btnCancelar.BackgroundImage = Resources.Undo_32x32;
            this.btnCancelar.BackgroundImageLayout = ImageLayout.Zoom;
            this.btnCancelar.Enabled = false;
            this.btnCancelar.Location = new Point(0x5c, 0);
            this.btnCancelar.Name = "btnCancelar";
            this.btnCancelar.Size = new Size(0x18, 0x18);
            this.btnCancelar.TabIndex = 9;
            this.btnCancelar.UseVisualStyleBackColor = true;
            this.btnCancelar.Click += new EventHandler(this.btnCancelar_Click);
            this.btnRemover.BackgroundImage = Resources.Delete_32x32;
            this.btnRemover.BackgroundImageLayout = ImageLayout.Zoom;
            this.btnRemover.Enabled = false;
            this.btnRemover.Location = new Point(0x20, 0);
            this.btnRemover.Name = "btnRemover";
            this.btnRemover.Size = new Size(0x18, 0x18);
            this.btnRemover.TabIndex = 8;
            this.btnRemover.UseVisualStyleBackColor = true;
            this.btnRemover.Click += new EventHandler(this.btnRemover_Click);
            this.btnAdd.BackgroundImage = Resources.Add_32x32;
            this.btnAdd.BackgroundImageLayout = ImageLayout.Zoom;
            this.btnAdd.Location = new Point(2, 0);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new Size(0x18, 0x18);
            this.btnAdd.TabIndex = 7;
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new EventHandler(this.btnAdd_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.btnSalvar);
            base.Controls.Add(this.btnCancelar);
            base.Controls.Add(this.btnRemover);
            base.Controls.Add(this.btnAdd);
            base.Name = "MiniCrud";
            base.Size = new Size(0x77, 0x18);
            base.ResumeLayout(false);
        }

        protected virtual void OnAddClicked()
        {
            if (this.AddClicked != null)
            {
                this.AddClicked();
            }
        }

        protected virtual void OnCancelClicked()
        {
            if (this.CancelClicked != null)
            {
                this.CancelClicked();
            }
        }

        protected virtual void OnGridSelected()
        {
            if (this.GridSelected != null)
            {
                this.GridSelected();
            }
        }

        protected virtual void OnRemoveClicked()
        {
            if (this.RemoveClicked != null)
            {
                this.RemoveClicked();
            }
        }

        protected virtual void OnUpdateClicked()
        {
            if (this.UpdateClicked != null)
            {
                this.UpdateClicked();
            }
        }

        private void PGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.selecionaGrid();
        }

        private void PGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.selecionaGrid();
        }

        public void rebindEventosGrid()
        {
            if (this.PGrid != null)
            {
                this.PGrid.CellClick += new DataGridViewCellEventHandler(this.PGrid_CellClick);
                this.PGrid.CellEnter += new DataGridViewCellEventHandler(this.PGrid_CellEnter);
            }
        }

        private void selecionaGrid()
        {
            try
            {
                if ((this.Grid.SelectedRows != null) && (this.Grid.SelectedRows.Count > 0))
                {
                    this.Entidade = (IEntidade)this.Grid.SelectedRows[0].DataBoundItem;
                    this.setEstadoAlteracaoMincrud(true);
                    this.OnGridSelected();
                }
            }
            catch
            {
            }
        }

        public void setEstadoAlteracaoMincrud(bool valor)
        {
            this.btnAdd.Enabled = !valor;
            this.btnCancelar.Enabled = valor;
            this.btnRemover.Enabled = valor;
            this.btnSalvar.Enabled = valor;
        }

        // Properties
        public IEntidade Entidade
        {
            get
            {
                return this.PEntidade;
            }
            set
            {
                this.PEntidade = value;
            }
        }

        public DataGridView Grid
        {
            get
            {
                return this.PGrid;
            }
            set
            {
                this.PGrid = value;
            }
        }

        public IList ListaRemovida
        {
            get
            {
                return this.PListaRemovida;
            }
            set
            {
                this.PListaRemovida = value;
            }
        }

        public FormPai Pai
        {
            get
            {
                return this.PPai;
            }
            set
            {
                this.PPai = value;
            }
        }

        // Nested Types
        public delegate void SubmitClickedHandler();
    }
}


